package com.example.shopp_2301.update

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.SendCodeEntity
import com.example.base.entity.SendCodeEntityResult
import com.example.base.entity.UpdatePwdEntityResult
import com.example.base.mvvm.BaseModel
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class UpdateViewModel:BaseViewModel() {
    //定义两个返回值 ---成功和失败
    val sendCodeFail = MutableLiveData<String>()
    val sendCodeSuccess = MutableLiveData<BaseModel<SendCodeEntity>>()
    //封装方法，实现调用
    fun sendCode(phone:String){
        viewModelScope.launch {
            try {
                sendCodeSuccess.value = apiServer.sendCode(phone)
            }catch (e:Exception){
                sendCodeFail.value = e.message
            }

        }
    }
    //返回值---修改密码
    val updatePwdSuccess = MutableLiveData<BaseModel<Boolean>>()
    val updatePwdFail = MutableLiveData<String>()
    fun updatePwd(account:String,captcha:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["account"] = account
                map["captcha"] = captcha
                map["password"] = password
                updatePwdSuccess.value = apiServer.updatePwd(mapToBody(map))
            }catch (e:Exception){
                updatePwdFail.value = e.message
            }
        }
    }
}